package hjt.com.module_test.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.tong.lib.utils.SizeUtils;

import androidx.annotation.Nullable;

public class ClockView extends View {

    private int defaultSize = SizeUtils.dp2px(300);
    private Paint circlePaint;
    private Paint textPaint;
    private int strokeWith = SizeUtils.dp2px(10);
    private Rect rect = new Rect();

    public ClockView(Context context) {
        this(context, null);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        circlePaint = new Paint();
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setStrokeWidth(25);
        circlePaint.setColor(Color.GREEN);
        circlePaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(12);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMax(widthMeasureSpec);
        int height = getMax(heightMeasureSpec);
        width = height = Math.min(width, height);
        setMeasuredDimension(width, height);
    }

    private int getMax(int sizeMeasureSpec) {
        int max = 0;
        int size = MeasureSpec.getSize(sizeMeasureSpec);
        switch (MeasureSpec.getMode(sizeMeasureSpec)) {
            case MeasureSpec.UNSPECIFIED:
                max = size;
                break;
            case MeasureSpec.AT_MOST:
                max = Math.min(defaultSize, size);
                break;
            case MeasureSpec.EXACTLY:
                max = size;
                break;
        }
        return max;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int radiu = getMeasuredWidth() / 2;
        int innearRadiu = radiu - strokeWith;

        canvas.translate(radiu, radiu);

        canvas.drawCircle(0, 0, radiu, circlePaint);

        circlePaint.setColor(Color.WHITE);
        canvas.drawCircle(0, 0, innearRadiu, circlePaint);

        for (int i = 0; i < 360; i += 6) {
            if (i % 30 == 0) {
                textPaint.setStrokeWidth(5);
                canvas.drawLine(0, innearRadiu, 0, innearRadiu - 50, textPaint);
            }else {
                textPaint.setStrokeWidth(2);
                canvas.drawLine(0, innearRadiu, 0, innearRadiu - 40, textPaint);
            }
            canvas.rotate(6);
        }

        float x, y;
        textPaint .setTextSize(SizeUtils.dp2px(12));
        for (int i = 1; i <= 12; i += 1) {
            textPaint.getTextBounds(String.valueOf(i), 0, String.valueOf(i).length(), rect);
            float textHeight = rect.height();
//            float distance = radiusIn - 2 * longLineLength - textHeight;
            float distance = 300;
            double tempVa = i * 30.0f * Math.PI / 180.0f;
            x = (float) (distance * Math.sin(tempVa));
            y = (float) (-distance * Math.cos(tempVa));
            canvas.drawText(String.valueOf(i), x, y + textHeight / 3, textPaint);
        }

        canvas.drawCircle(0,0,10,textPaint);
        textPaint.setColor(Color.RED);
        canvas.drawLine(0,200,0,-50,textPaint);


    }
}
